<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/20
 * Time: 10:50
 */

namespace app\admin\controller;


use think\Db;
use think\Request;
class Grade extends Base {
    protected $grade_names = [
        '',
        '小班','中班','大班',
        '一年级','二年级','三年级','四年级','五年级','六年级',
        '初一','初二','初三',
        '高一','高二','高三',
        '大一', '大二', '大三', '大四'
    ];
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($keyword) && $keyword != ''){
                $where['school_erp|school_name'] = ['like', '%' . $keyword . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('school_grade')->alias('a')
                ->join('up_school us','us.school_erp =a.grade_school_erp','left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('school_grade')->alias('a')
                ->join('up_school us','us.school_erp =a.grade_school_erp','left')
                ->where($where)
                ->order('grade_school_erp,grade_level')
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function add(){
        if(Request::instance()->isPost()){
            $grade_level = trim(input('grade_level'), ' ');
            $grade_school_erp = input('grade_school_erp');
            if(!$grade_level) return json(['code' => 100, 'msg' => '请选择年级']);
            if(!$grade_school_erp) return json(['code' => 100, 'msg' => '请选择学校']);
            $grade_names = $this->grade_names;
            $grade_level = explode(',',$grade_level);
            $data = [];
            $have = Db::table('up_school_grade')
                ->where('grade_school_erp',$grade_school_erp)
                ->where('grade_level','in',$grade_level)
                ->find();
            if($have) return json(['code' => 100, 'msg' => '有重复的年级']);
            foreach ($grade_level as $k => $v){
                $data[] = [
                    'grade_name' => $grade_names[$v],
                    'grade_school_erp' => $grade_school_erp,
                    'grade_level' => $v,
                    'grade_time' => date('Y-m-d H:i:s'),
                    'grade_update_time' => date('Y-m-d H:i:s')
                ];
            }

            $res = Db::table('up_school_grade')->insertAll($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $school = Db::table('up_school')
            ->field('school_id, school_name, school_erp')
            ->select();
        $this->assign([
            'schools' => $school
        ]);
        return $this->fetch();
    }

    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function update(){
        if(Request::instance()->isPost()){
            $grade_id = input('grade_id');
            $grade_level = trim(input('grade_level'), ' ');
            $grade_names = $this->grade_names;
            $data = [
                'grade_name' => $grade_names[$grade_level],
                'grade_level' => $grade_level,
                'grade_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_school_grade')->where('grade_id', $grade_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $grade_id = input('grade_id');
        // banner
        $banner = Db::table('up_school_grade')
            ->where('grade_id', $grade_id)
            ->find();
        $this->assign([
            'grade_id' => $grade_id,
            'data' => $banner
        ]);
        return $this->fetch();
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function del(){
        $banner_id = input('id');
        $res = Db::table('up_school_grade')->where('grade_id', $banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 200, 'msg' => '失败']);
        }
    }
}